<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Bill extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('bill_mdl');
        $this->load->model('uinfo_mdl');
    }

    /**
     * 账单记录
     */
    public function index(){
        //
    }

    /**
     * 查看账单
     */
    public function show_bill(){
        $uid = $this->input->get('uid');

        if(empty($uid)){
            if($this->session->group == 3){
                $uid = $_SESSION['user']['id'];
            }else{
                show_error('参数错误');
            }
        }

        $this->db->start_cache();
        $this->db->where('user_id',intval($uid));
        $this->db->stop_cache();

        $this->load->library('pagination');

        $config['base_url'] = site_url('bill/show_bill?uid='.$uid);
        $config['total_rows'] = $this->bill_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('update_time desc');
        $data['list'] = $this->bill_mdl->select();
        $this->db->flush_cache();

        if($this->session->group == 3){
            $this->_view('show',$data);
        }else{
            $this->load->view('default/bill/show_bill',$data);
        }
    }

    /**
     * 查询充值记录
     */
    public function bill_log(){
        if($this->session->group == 3){
            show_error('没有权限！');
        }

        $this->db->start_cache();
        $this->db->where('charges >',0);
        $this->db->stop_cache();

        $this->load->library('pagination');

        $config['base_url'] = site_url('bill/bill_log');
        $config['total_rows'] = $this->bill_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('update_time desc,id desc');
        $list = $this->bill_mdl->select();
        $this->db->flush_cache();

        /** 查询用户信息 */
        $this->load->model('users_mdl');
        foreach($list as $k => $v){
            $this->db->where('id',$v['user_id']);
            $this->db->select('username');
            $r = $this->users_mdl->get_one();
            $list[$k]['name'] = $r['username'];
        }
        $data['list'] = $list;
        $this->_view('log',$data);
    }

    /**
     * 用户充值
     */
    public function recharge(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }

        if($_SESSION['user']['status'] != 1){
            die('没有权限！');
        }

        $post = $this->input->post();

        if($post['money'] < 0){
            die('请输入一个充值的正数');
        }

        $this->db->trans_start();
        $this->db->where('id',intval($post['uid']));
        $this->db->set('money','money+'.intval($post['money']),FALSE);
        $this->uinfo_mdl->update();
        $data = array(
            'user_id' => intval($post['uid']),
            'charges' => intval($post['money']),
            'remarks' => '后台人工充值'.$post['money'],
            'update_time' => time()
        );
        $res = $this->bill_mdl->create($data);

        $this->load->model('users_mdl');
        $this->users_mdl->set_group($data['user_id']);
        $this->db->trans_complete();

        echo $res == true ? '1' : '操作失败！';
    }

}

/* End of file Bill.php */
/* Location: ./application/controllers/Bill.php */